package com.polarrose.wsf.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpValidator implements Validator<String> {

	private Pattern pattern;

	public void validate(String o)
        throws ValidatorException
    {
		if (pattern == null) {
			throw new RuntimeException("Pattern cannot be null in regexp validator!");
		}

		Matcher matcher = pattern.matcher(o);
		if (!matcher.matches()) {
			throw new ValidatorException("Parameter must match regular expression pattern " + pattern.pattern());
		}
	}

	public void setPattern(String pattern) {
		this.pattern = Pattern.compile(pattern);
	}
}
